package com.cxx.purchasecharge.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.cxx.purchasecharge.core.model.persistence.Contact;
import com.cxx.purchasecharge.core.model.persistence.Customer;
import com.cxx.purchasecharge.core.model.persistence.Goods;
import com.cxx.purchasecharge.core.model.persistence.GoodsStorage;
import com.cxx.purchasecharge.core.model.persistence.Order;
import com.cxx.purchasecharge.core.model.persistence.Payment;
import com.cxx.purchasecharge.core.model.persistence.User;
import com.cxx.purchasecharge.dal.ContactDao;
import com.cxx.purchasecharge.dal.CustomerDao;
import com.cxx.purchasecharge.dal.GoodsDao;
import com.cxx.purchasecharge.dal.GoodsStorageDao;
import com.cxx.purchasecharge.dal.OrderDao;
import com.cxx.purchasecharge.dal.PaymentDao;
import com.cxx.purchasecharge.dal.UserDao;
import com.cxx.purchasecharge.service.CoreService;
import com.cxx.purchasecharge.service.PersistenceService;
import com.cxx.purchasecharge.service.exception.PcServiceException;

@Transactional
public class PersistenceServiceImpl implements PersistenceService
{
    private CoreService coreService;
    private CustomerDao customerDao;
    private OrderDao orderDao;
    private ContactDao contactDao;
    private UserDao userDao;
    private GoodsStorageDao storageDao;
    private PaymentDao paymentDao;
    private GoodsDao goodsDao;

    @Override
    public Customer addCustomer (Customer customer) throws PcServiceException
    {
        return customerDao.save (customer);
    }

    @Override
    public Customer updateCustomer (Customer customer) throws PcServiceException
    {
        return customerDao.save (customer);
    }

    @Override
    public void deleteCustomer (Customer customer) throws PcServiceException
    {
        customerDao.delete (customer);
    }

    @Override
    public void deleteCustomer (List <Customer> customers) throws PcServiceException
    {
        customerDao.delete (customers);
    }

    @Override
    public Contact addContact (Contact contact) throws PcServiceException
    {
        return contactDao.save (contact);
    }

    @Override
    public Contact updateContact (Contact contact) throws PcServiceException
    {
        return contactDao.save (contact);
    }

    @Override
    public void deleteContact (Contact contact) throws PcServiceException
    {
        contactDao.delete (contact);
    }

    @Override
    public void deleteContact (List <Contact> contacts) throws PcServiceException
    {
        contactDao.delete (contacts);
    }

    @Override
    public User addUser (User user) throws PcServiceException
    {
        return userDao.save (user);
    }

    @Override
    public User updateUser (User user) throws PcServiceException
    {
        return userDao.save (user);
    }

    @Override
    public void deleteUser (User user) throws PcServiceException
    {
        userDao.delete (user);
    }

    @Override
    public void deleteUser (List <User> users) throws PcServiceException
    {
        userDao.delete (users);
    }

    @Override
    public Goods addGoods (Goods goods) throws PcServiceException
    {
        return goodsDao.save (goods);
    }

    @Override
    public Goods updateGoods (Goods goods) throws PcServiceException
    {
        return goodsDao.save (goods);
    }

    @Override
    public void deleteGoods (Goods goods) throws PcServiceException
    {
        goodsDao.delete (goods);
    }

    @Override
    public void deleteGoods (List <Goods> goodses) throws PcServiceException
    {
        goodsDao.delete (goodses);
    }

    @Override
    public GoodsStorage addStorage (GoodsStorage storage) throws PcServiceException
    {
        return storageDao.save (storage);
    }

    @Override
    public GoodsStorage updateStorage (GoodsStorage storage) throws PcServiceException
    {
        return storageDao.save (storage);
    }

    @Override
    public void deleteStorage (GoodsStorage storage) throws PcServiceException
    {
        storageDao.delete (storage);
    }

    @Override
    public void deleteStorage (List <GoodsStorage> storages) throws PcServiceException
    {
        storageDao.delete (storages);
    }

    @Override
    public Order addOrder (Order order) throws PcServiceException
    {
        return coreService.saveOrder (order);
    }

    @Override
    public Order updateOrder (Order order) throws PcServiceException
    {
        return orderDao.save (order);
    }

    @Override
    public void deleteOrder (Order order) throws PcServiceException
    {
        orderDao.delete (order);
    }

    @Override
    public void deleteOrder (List <Order> orders) throws PcServiceException
    {
        orderDao.delete (orders);
    }

    @Override
    public Payment addPayment (Payment payment) throws PcServiceException
    {
        Payment pay = paymentDao.save (payment);
        Customer customer = customerDao.findOne (pay.getCustomer ().getId ());
        float dealMoney = customer.getDealMoney () - pay.getPaid ();
        customerDao.updateDealMoney (dealMoney, customer.getId ());
        return pay;
    }

    @Override
    public Payment updatePayment (Payment payment) throws PcServiceException
    {
        return paymentDao.save (payment);
    }

    @Override
    public void deletePayment (Payment payment) throws PcServiceException
    {
        paymentDao.delete (payment);
    }

    @Override
    public void deletePayment (List <Payment> payments) throws PcServiceException
    {
        paymentDao.delete (payments);
    }

    public void setCoreService (CoreService coreService)
    {
        this.coreService = coreService;
    }

    public void setCustomerDao (CustomerDao customerDao)
    {
        this.customerDao = customerDao;
    }

    public void setOrderDao (OrderDao orderDao)
    {
        this.orderDao = orderDao;
    }

    public void setContactDao (ContactDao contactDao)
    {
        this.contactDao = contactDao;
    }

    public void setUserDao (UserDao userDao)
    {
        this.userDao = userDao;
    }

    public void setStorageDao (GoodsStorageDao storageDao)
    {
        this.storageDao = storageDao;
    }

    public void setPaymentDao (PaymentDao paymentDao)
    {
        this.paymentDao = paymentDao;
    }

    public void setGoodsDao (GoodsDao goodsDao)
    {
        this.goodsDao = goodsDao;
    }

}
